package com.android.camera.manager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Display;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.graphics.Color;

import android.graphics.drawable.BitmapDrawable;
import android.hardware.Camera.Parameters;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.android.camera.CameraActivity;
import com.android.camera.CameraHolder;
import com.android.camera.Log;
import com.android.camera.R;
import com.android.camera.bridge.CameraAppUiImpl;
import com.android.camera.bridge.ParametersExt;
import com.mediatek.camera.ISettingCtrl;
import com.mediatek.camera.platform.ICameraAppUi.CommonUiType;
import com.mediatek.camera.platform.ICameraAppUi.ViewState;
import com.mediatek.camera.setting.SettingConstants;
import com.mediatek.camera.setting.SettingDataBase;
import com.mediatek.camera.setting.preference.CameraPreference;
import com.mediatek.camera.setting.preference.ListPreference;
import com.mediatek.camera.setting.preference.PreferenceGroup;

import com.android.camera.ui.RotateImageView;
import com.android.camera.ui.optionseek.RangeBar;
import com.android.camera.ui.optionseek.RangeBar.OnRangeBarChangeListener;
import com.android.camera.ui.optionseek.RangeBar.OnRangeBarTextListener;
import com.android.camera.ui.RGK_ArcSeekBar;
import com.android.camera.ui.RGK_ArcSeekBar.CenterPoint;
import com.android.camera.ui.RGK_ArcSeekBar.OnSeekBarChangeListener;

import com.android.camera.ui.optionseek.PinView;

public class RGKProManager extends ViewManager implements
        CameraActivity.OnPreferenceReadyListener, CameraActivity.OnParametersReadyListener {

    private static final String TAG = "CameraProManager";
    private static final String AUTO = "auto";
    private static final String MANUAL = "manual";
    private static final String AF = "AF";
    private static final String MF = "MF";
    private static final String PARA_EXPOSURE_TIME = "shutter-value";
    private static final String KEY_EXPOSURE_TIME = "prof_exposure_time_key";
    private static final String KEY_MANUAL_FOCUS = "prof_manual_focus_key";
    private static final String WB_AUTO = "auto";
    private static final String WB_INCANDESCENT = "incandescent";
    private static final String WB_FLUORESCENT = "fluorescent";
    private static final String WB_DAYLIGHT = "daylight";
    private static final String WB_CLOUDY = "cloudy-daylight";
    private static final String WB_TWILIGHT = "twilight";
    private static final String WB_SHADE = "shade";
    private static final int PROF_OPTION_EV = 0;
    private static final int PROF_OPTION_ISO = 1;
    private static final int PROF_OPTION_WB = 2;
    private static final int PROF_OPTION_ET = 3;
    private static final int PROF_OPTION_MF = 4;
    private static final int PROF_OPTION_TOTOL = 5;

    private int mCurrentOption = PROF_OPTION_WB;

    private CameraActivity mCamera;
    private CameraAppUiImpl mCameraAppUi;
    private ISettingCtrl mSettingController;
    private ParametersExt mParametersExt;
    private Parameters mParameters;

    private boolean isFirstShowContainer = true;
    private boolean mMfListInited;

    private int mEvIndex;
    private int mIsoIndex;
    private int mWbIndex;
    private int mEtIndex;
    private int mMfIndex;

    private View mProLayout;
    private RangeBar mOptionBar;
    private TextView mCurrent;
    private TextView mEvView;
    private TextView mIsoView;
    private TextView mWbView;
    private TextView mEtView;
    private TextView mMfView;
    private ArrayList<TextView> mOptionViews = new ArrayList<>();

    private ArrayList<Integer> mEvList;
    private ArrayList<String> mIsoList;
    private ArrayList<String> mWbList;
    private ArrayList<Bitmap> mWbIconList = new ArrayList<>();
    private ArrayList<Integer> mEtList = new ArrayList<Integer>() {
        {
            add(0);
            add(2);
            add(10);
            add(200);
            add(4000);
            add(8000);
        }
    };
    private ArrayList<Integer> mMfList = new ArrayList<Integer>();
    private ArrayList<String> mWbListFilter = new ArrayList<String>() {
        {
            add(WB_AUTO);
            add(WB_INCANDESCENT);
            add(WB_FLUORESCENT);
            add(WB_DAYLIGHT);
            add(WB_CLOUDY);
            add(WB_TWILIGHT);
            add(WB_SHADE);
        }
    };

    private int mCurrentEv;
    private String mCurrentIso;
    private String mCurrentWb;
    private int mCurrentEt;
    private int mCurrentMf;
    private int mFocusMaxStep;

    private Display display;
    private Animation mFadeIn;
    private Animation mFadeOut;
    private boolean mCancelHideAnimation = false;

    private static final float F = 0.51f;
    private static final float f = 0.01f;

    private OnRangeBarTextListener mOptionBarPinViewListener = new OnRangeBarTextListener() {

        @Override
        public String getPinValue(RangeBar rangeBar, int tickIndex) {
            String pinValue = "";
            switch (mCurrentOption) {
                case PROF_OPTION_EV:
                    if (tickIndex >= 0 && tickIndex < mEvList.size()) {
                        pinValue = mEvList.get(tickIndex) + "";
                    }
                    break;
                case PROF_OPTION_ISO:
                    if (tickIndex >= 0 && tickIndex < mIsoList.size()) {
                        pinValue = mIsoList.get(tickIndex);
                    }
                    break;
                case PROF_OPTION_WB:
                    if (tickIndex >= 0 && tickIndex < mWbList.size()) {
                        pinValue = mWbList.get(tickIndex);
                    }
                    break;
                case PROF_OPTION_ET:
                    if (tickIndex >= 0 && tickIndex < mEtList.size()) {
                        pinValue = transMsToS(mEtList.get(tickIndex));
                    }
                    break;
                case PROF_OPTION_MF:
                    if (tickIndex >= 0 && tickIndex < mMfList.size()) {
                        int value = mMfList.get(tickIndex);
                        if (value == mFocusMaxStep) { //> mFocusMaxStep * 99 / 100) {
                            pinValue = AF;
                        } else {
                            pinValue = MF;
                        }
                    }
                    break;
                default:
                    break;
            }
            return pinValue;
        }

        @Override
        public Bitmap getPinIcon(RangeBar rangeBar, int tickIndex) {
            if (mCurrentOption == PROF_OPTION_WB) {
                if (tickIndex >= 0 && tickIndex < mWbIconList.size()) {
                    return mWbIconList.get(tickIndex);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    };

    private OnRangeBarChangeListener mOptionBarChangeListener = new OnRangeBarChangeListener() {

        @Override
        public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex,
                String leftPinValue, String rightPinValue) {
            onValueChanged(rightPinIndex);
        }
    };

    private class OptionOnClickListener implements OnClickListener {

        int mIndex;

        public OptionOnClickListener(int index) {
            mIndex = index;
        }

        @Override
        public void onClick(View v) {
            if (mIndex == PROF_OPTION_MF && mMfList.size() == 0) {
                return;
            }
            refreshOptions(mIndex);
        }
    };

    public RGKProManager(CameraActivity context) {
        super(context, VIEW_LAYER_MODULES);
        mCamera = context;
        context.addOnPreferenceReadyListener(this);
        context.addOnParametersReadyListener(this);
    }

    @Override
    protected View getView() {
        mProLayout = inflate(R.layout.camera_mode_prof);
        mCurrent = (TextView) mProLayout.findViewById(R.id.option_current);
        mOptionBar = (RangeBar) mProLayout.findViewById(R.id.optionbar);
        mOptionBar.setPinTextListener(mOptionBarPinViewListener);
        mOptionBar.setOnRangeBarChangeListener(mOptionBarChangeListener);

        mEvView = (TextView) mProLayout.findViewById(R.id.prof_ev);
        mIsoView = (TextView) mProLayout.findViewById(R.id.prof_iso);
        mWbView = (TextView) mProLayout.findViewById(R.id.prof_wb);
        mEtView = (TextView) mProLayout.findViewById(R.id.prof_et);
        mMfView = (TextView) mProLayout.findViewById(R.id.prof_mf);
        mOptionViews.add(mEvView);
        mOptionViews.add(mIsoView);
        mOptionViews.add(mWbView);
        mOptionViews.add(mEtView);
        mOptionViews.add(mMfView);
        showPro();
        return mProLayout;
    }

    @Override
    public void onCameraParameterReady() {

    }

    @Override
    public void onPreferenceReady() {

    }

    @Override
    public void hide() {
        resetCameraParameters();
        super.hide();
    }

    private void setWbIcons() {
        mWbIconList.clear();
        for (int i = 0; i < mWbList.size(); i++) {
            String value = mWbList.get(i);
            int res = -1;
            if (WB_AUTO.equals(value)) {
                res = R.drawable.ic_white_balance_auto_holo_light;
            } else if (WB_INCANDESCENT.equals(value)) {
                res = R.drawable.ic_white_balance_incandescent_holo_light;
            } else if (WB_FLUORESCENT.equals(value)) {
                res = R.drawable.ic_white_balance_fluorescent_holo_light;
            } else if (WB_DAYLIGHT.equals(value)) {
                res = R.drawable.ic_white_balance_sunlight_holo_light;
            } else if (WB_CLOUDY.equals(value)) {
                res = R.drawable.ic_white_balance_cloudy_holo_light_am;
            } else if (WB_TWILIGHT.equals(value)) {
                res = R.drawable.ic_white_balance_twlight_holo_light_am;
            } else if (WB_SHADE.equals(value)) {
                res = R.drawable.ic_white_balance_shade_holo_light;
            }
            if (res != -1) {
                mWbIconList.add(BitmapFactory.decodeResource(mCamera.getResources(), res));
            }
        }
    }

    private void setOptionBar() {
        int optionCount = 0;
        int currentIndex = 0;
        boolean drawTicksUnselected = true;
        switch (mCurrentOption) {
            case PROF_OPTION_EV:
                optionCount = mEvList.size();
                currentIndex = mEvIndex;
                break;
            case PROF_OPTION_ISO:
                optionCount = mIsoList.size();
                currentIndex = mIsoIndex;
                break;
            case PROF_OPTION_WB:
                optionCount = mWbList.size();
                currentIndex = mWbIndex;
                break;
            case PROF_OPTION_ET:
                optionCount = mEtList.size();
                currentIndex = mEtIndex;
                break;
            case PROF_OPTION_MF:
                optionCount = mMfList.size();
                currentIndex = mMfIndex;
                drawTicksUnselected = false;
                break;
            default:
                break;
        }
        if (optionCount < 2) {
            return;
        }
        mOptionBar.setDrawTicksUnselected(drawTicksUnselected);
        mOptionBar.setTickStart(0);
        mOptionBar.setTickEnd(optionCount - 1);
        mOptionBar.setRangePinsByIndices(0, currentIndex);

        for (int i = 0; i < mOptionViews.size(); i++) {
            if (i == mCurrentOption) {
                mOptionViews.get(i).setSelected(true);
            } else {
                mOptionViews.get(i).setSelected(false);
            }
            mOptionViews.get(i).setOnClickListener(new OptionOnClickListener(i));
        }
        if (mCurrentOption != PROF_OPTION_MF) {
            mCurrent.setText(mOptionViews.get(mCurrentOption).getText());
        } else {
            if (mMfIndex == 0) {
                mCurrent.setText(AF);
            } else {
                mCurrent.setText(MF);
            }
        }
    }

    public void cancelHideAnimation() {
        mCancelHideAnimation = true;
    }

    private void initMfList() {
        boolean isFocusAreaSupported = mCamera.getFocusManager().getFocusAreaSupported();
        mMfList.clear();
        if (isFocusAreaSupported) {
            mFocusMaxStep = mCamera.getFocusMaxStep();
            Log.d(TAG, "MF is supported, mFocusMaxStep=" + mFocusMaxStep);
            for (int i = mFocusMaxStep; i >= 0; i--) {
                mMfList.add(i);
            }
        } else {
            mFocusMaxStep = 0;
            Log.d(TAG, "MF is NOT supported");
        }
        refreshMf();
    }

    private void resetCameraParameters() {
        // FengYibin, professional mode uses same setting share preferences.
        // So can NOT restore exposure, exposure time and white balance.
        if (mEtIndex != 0) {
            mParametersExt.set(PARA_EXPOSURE_TIME, mEtList.get(0));
        }
        if (mMfIndex != 0) {
            mCamera.setMF(AUTO);
        }
    }

    public void showPro() {
        mParametersExt = mCamera.getCameraDeviceCtrl().getParametersExt();
        mParameters = mCamera.getParameters();
        if (!mMfListInited) {
            // initialize cameraPro some parameters
            initParametersLists();
            mMfListInited = true;
        }
        getCameraProParameters(true);
        setSharedPreferences();
        setOptionBar();
    }

    public void update() {
        if (isShowing()) {
            if (mParametersExt == null) {
                mParametersExt = mCamera.getCameraDeviceCtrl().getParametersExt();
            }
            initParametersLists();
            getCameraProParameters(true);
            setSharedPreferences();
            setOptionBar();
        }
    }

    protected void startFadeInAnimation(View view) {
        if (mFadeIn == null) {
            mFadeIn = AnimationUtils.loadAnimation(getContext(), R.anim.setting_popup_grow_fade_in);
        }
        if (view != null && mFadeIn != null) {
            view.startAnimation(mFadeIn);
        }
    }

    protected void startFadeOutAnimation(View view) {
        if (mFadeOut == null) {
            mFadeOut = AnimationUtils.loadAnimation(getContext(),
                    R.anim.setting_popup_shrink_fade_out);
        }
        if (view != null && mFadeOut != null) {
            view.startAnimation(mFadeOut);
        }
    }

    public void setSettingController(ISettingCtrl settingController) {
        mSettingController = settingController;
    }

    private void onValueChanged(int index) {
        String key = "";
        String value = "";
        switch (mCurrentOption) {
            case PROF_OPTION_EV:
                mEvIndex = index;
                key = SettingConstants.KEY_EXPOSURE;
                if (index >= 0 && index < mEvList.size()) {
                    value = mEvList.get(index).toString();
                }
                break;
            case PROF_OPTION_ISO:
                mIsoIndex = index;
                key = SettingConstants.KEY_ISO;
                if (index >= 0 && index < mIsoList.size()) {
                    value = mIsoList.get(index);
                }
                break;
            case PROF_OPTION_WB:
                mWbIndex = index;
                key = SettingConstants.KEY_WHITE_BALANCE;
                if (index >= 0 && index < mWbList.size()) {
                    value = mWbList.get(index);
                }
                break;
            case PROF_OPTION_ET:
                mEtIndex = index;
                key = KEY_EXPOSURE_TIME;
                if (index >= 0 && index < mEtList.size()) {
                    int time = mEtList.get(index);
                    value = transMsToS(time);
                }
                break;
            case PROF_OPTION_MF:
                mMfIndex = index;
                key = KEY_MANUAL_FOCUS;
                if (index >= 0 && index < mMfList.size()) {
                    int mfValue = mMfList.get(index);
                    value = transMfToF(mfValue);
                }
                break;
            default:
                break;
        }

        if (mCurrentOption == PROF_OPTION_ET || mCurrentOption == PROF_OPTION_MF) {
            switch (mCurrentOption) {
                case PROF_OPTION_ET:
                    int exposureTime = Integer.parseInt(transStoMs(value));
                    Log.i("EXPOSURE_TIME", transStoMs(value));
                    Log.i("exposureTime", exposureTime + "");
                    mParametersExt.set(PARA_EXPOSURE_TIME, exposureTime);
                    break;
                case PROF_OPTION_MF:
                    mCamera.setMF(value);
                    if (AUTO.equals(value)) {
                        mCurrent.setText(AF);
                    } else {
                        mCurrent.setText(MF);
                    }
                    break;
            }
            mCamera.getCameraDeviceCtrl().applyParameters(false);
            setSharedPreferences(value);
        } else {
            ListPreference pref = mSettingController.getListPreference(key);
            if (pref == null) {
                return;
            }
            pref.setValue(value);
//            pref.setValueIndex(index);
            pref.setOverrideValue(null, false);
            if (!mCamera.isCameraOpened()) {
                return;
            }
//            String value = pref.getValue();
            mSettingController.onSettingChanged(key, value);
            mCamera.getCameraDeviceCtrl().applyParameters(false);
        }
    }

    private String transMfToF(int mf) {
        String curMfStr = null;
        if (mf == mFocusMaxStep) {// (mf > mFocusMaxStep * 99 / 100) {
            curMfStr = AUTO;
        } else {
            curMfStr = mf + "";
        }
        return curMfStr;
    }

    private int transFToMf(String f) {
        int curMf = 0;
        if (f.equals(AUTO)) {
            curMf = mFocusMaxStep;// mMfList.size()-1;
        } else {
            curMf = Integer.parseInt(f);
        }
        return curMf;
    }

    private void setSharedPreferences() {
        SharedPreferences sharePreferences = mCamera.getSharePreferences();
        Editor editor = sharePreferences.edit();
        editor.putString(SettingConstants.KEY_EXPOSURE, mCurrentEv + "");
        editor.putString(SettingConstants.KEY_ISO, mCurrentIso);
        editor.putString(SettingConstants.KEY_WHITE_BALANCE, mCurrentWb);
        editor.putString(KEY_EXPOSURE_TIME, transMsToS(mCurrentEt));
        editor.putString(KEY_MANUAL_FOCUS, transMfToF(mCurrentMf));
        editor.apply();
        mCamera.getISettingCtrl().updateSetting(mCamera.getPreferences().getLocal());
    }

    private void setSharedPreferences(String value) {
        SharedPreferences sharePreferences = mCamera.getSharePreferences();
        Editor editor = sharePreferences.edit();
        boolean updateSetting = true;
        switch (mCurrentOption) {
            case PROF_OPTION_EV:
                editor.putString(SettingConstants.KEY_EXPOSURE, value);
                break;
            case PROF_OPTION_ISO:
                editor.putString(SettingConstants.KEY_ISO, value);
                break;
            case PROF_OPTION_WB:
                editor.putString(SettingConstants.KEY_WHITE_BALANCE, value);
                break;
            case PROF_OPTION_ET:
                editor.putString(KEY_EXPOSURE_TIME, value);
                updateSetting = false;
                break;
            case PROF_OPTION_MF:
                editor.putString(KEY_MANUAL_FOCUS, value);
                updateSetting = false;
                break;
            default:
                break;
        }
        editor.apply();
        if (updateSetting) {
            mCamera.getISettingCtrl().updateSetting(mCamera.getPreferences().getLocal());
        }
    }

    private String getCurEffectValue(int listIndex) {
        String curValue = "";
        switch (mCurrentOption) {
            case PROF_OPTION_EV:
                if (listIndex >= 0 && listIndex < mEvList.size()) {
                    curValue = mEvList.get(listIndex).toString();
                }
                break;

            case PROF_OPTION_ISO:
                if (listIndex >= 0 && listIndex < mIsoList.size()) {
                    curValue = mIsoList.get(listIndex);
                }
                break;

            case PROF_OPTION_WB:
                if (listIndex >= 0 && listIndex < mWbList.size()) {
                    curValue = mWbList.get(listIndex);
                }
                break;

            case PROF_OPTION_ET:
                if (listIndex >= 0 && listIndex < mEtList.size()) {
                    int time = mEtList.get(listIndex);
                    curValue = transMsToS(time);
                }
                break;

            case PROF_OPTION_MF:
                if (listIndex >= 0 && listIndex < mMfList.size()) {
                    int mfValue = mMfList.get(listIndex);
                    curValue = transMfToF(mfValue);
                }
                break;
            default:
                break;
        }
        return curValue;
    }

    private String transMsToS(int ms) {
        if (ms == 0) {
            return AUTO;
        }
        if (ms >= 1000) {
            return (ms / 1000) + "";
        }
        return ms == 1 ? "1/1000"
                : ms == 2 ? "1/500"
                        : ms == 5 ? "1/200" : ms == 10 ? "1/100" : ms == 50 ? "1/20" : "1/5";
    }

    public String transStoMs(String s) {
        Log.i("transStoMs_s", s);
        if (s.equals(AUTO)) {
            return "0";
        }

        if (!s.contains("/")) {
            return s + "000";
        }
        return s.equals("1/1000") ? "1"
                : s.equals("1/500") ? "2"
                        : s.equals("1/200") ? "5"
                                : s.equals("1/100") ? "10" : s.equals("1/20") ? "50" : "200";
    }

    @Override
    public void onRefresh() {
        update();
    }

    private void initParametersLists() {
        initMfList();
        initExposureValues();
        mIsoList = (ArrayList<String>) mParametersExt.getSupportedISOSpeed();
        mWbList = (ArrayList<String>) mParametersExt.getSupportedWhiteBalance();
        filterWbEffect(mWbList);
        filerIsoEffect(mIsoList);
        Log.i(TAG, "initCameraProParameter(),mIsoList.size" + mIsoList.size() + "mWbList"
                + mWbList.size() + "mEtList" + mEtList.size());
    }

    private void getCameraParameters() {
        if (mParametersExt == null) {
            return;
        }
        mCurrentIso = mCamera.getISettingCtrl().getSettingValue(SettingConstants.KEY_ISO);
        mCurrentWb = mCamera.getISettingCtrl().getSettingValue(SettingConstants.KEY_WHITE_BALANCE);
        try {
            mCurrentEv = Integer.parseInt(mCamera.getISettingCtrl().getSettingValue(SettingConstants.KEY_EXPOSURE));
            mCurrentEt = Integer.parseInt(mParametersExt.get(PARA_EXPOSURE_TIME));
        } catch (Exception e) {
            // TODO: handle exception
        }
        mCurrentMf = transFToMf(mCamera.getMf());
        Log.i(TAG, "mCurrentEv=" + mCurrentEv + ", mCurrentIso=" + mCurrentIso + ", mCurrentWb="
                + mCurrentWb + ", mCurrentEt=" + mCurrentEt + ", mCurrentMf=" + mCurrentMf);
    }

    private void getParametersFromSharePref() {
        SharedPreferences sharePreferences = mCamera.getSharePreferences();
        mCurrentEv = Integer.parseInt(
                sharePreferences.getString(SettingConstants.KEY_EXPOSURE,
                        mCamera.getString(SettingDataBase
                                .getDefaultSettingID(SettingConstants.ROW_SETTING_EXPOSURE))));
        mCurrentIso = sharePreferences.getString(SettingConstants.KEY_ISO,
                mCamera.getString(
                        SettingDataBase.getDefaultSettingID(SettingConstants.ROW_SETTING_ISO)));
        mCurrentWb = sharePreferences.getString(SettingConstants.KEY_WHITE_BALANCE,
                mCamera.getString(SettingDataBase
                        .getDefaultSettingID(SettingConstants.ROW_SETTING_WHITE_BALANCE)));
        mCurrentEt = Integer.parseInt(
                transStoMs(sharePreferences.getString(KEY_EXPOSURE_TIME, AUTO)));
        if (!mParametersExt.getFocusMode().equals(MANUAL)) {
            Editor editor = sharePreferences.edit();
            editor.putString(KEY_MANUAL_FOCUS, AUTO);
            editor.apply();
        }
        mCurrentMf = transFToMf(sharePreferences.getString(KEY_MANUAL_FOCUS, AUTO));
        Log.i("getParametersFromSharePref",
                sharePreferences.getString(KEY_MANUAL_FOCUS, AUTO) + ", mCurrentMf" + mCurrentMf);
    }

    private void getCameraProParameters(boolean fromSharePref) {
        if (fromSharePref) {
            getParametersFromSharePref();
        } else {
            getCameraParameters();
        }

        String curEtByS = transMsToS(mCurrentEt);
        String curMfStr = transMfToF(mCurrentMf);
        Log.i(TAG, "initCameraProParameter(), mCurrentEv" + mCurrentEv + ", mCurrentIso" + mCurrentIso + ", mCurrentWb" + mCurrentWb
                + ", mCurrentEt" + mCurrentEt + ", mCurrentMf" + mCurrentMf + "curMfStr" + curMfStr);

        mEvIndex = findProIndex(mCurrentEv, mEvList);
        mIsoIndex = findProIndex(mCurrentIso, mIsoList);
        mWbIndex = findProIndex(mCurrentWb, mWbList);
        mEtIndex = findProIndex(mCurrentEt, mEtList);
        mMfIndex = findProIndex(mCurrentMf, mMfList);

        Log.i(TAG, "getCameraProParameters(), mEvIndex" + mEvIndex + ", mIsoIndex" + mIsoIndex + ", mWbIndex"
                + mWbIndex + ", mEtIndex" + mEtIndex + ", mMfIndex" + mMfIndex);
    }

    private void initExposureValues() {
        Log.i(TAG, "initExposureValues,maxEv start");
        if (mParametersExt != null) {
            int maxEv = mParametersExt.getMaxExposureCompensation();
            int minEv = mParametersExt.getMinExposureCompensation();
            float stepEv = mParametersExt.getExposureCompensationStep();
            Log.i(TAG, "initExposureValues,stepEv" + stepEv);

            mEvList = new ArrayList<Integer>();

            int len = (int) ((maxEv - minEv) / stepEv + 1);
            for (int i = 0; i < len; i++) {
                int tempEv = (int) (minEv + i * stepEv);
                mEvList.add(tempEv);
            }
            Log.i(TAG, "mEvList.size()" + mEvList.size());
        }
    }

    private void filerIsoEffect(List<String> mIsoList) {
        Log.i("filerIsoEffect", "mIsoList_size" + mIsoList.size());
        Iterator<String> iter = mIsoList.iterator();
        while (iter.hasNext()) {
            String s = iter.next();
            if (!s.equals(AUTO)) {
                if (Integer.parseInt(s) > 800) {
                    iter.remove();
                }
            }
        }
        Log.i("filerIsoEffect", "mIsoList_size" + mIsoList.size());
    }

    private void filterWbEffect(List<String> mWbList) {
        Log.i("filterWbEffect", "mWbList_size" + mWbList.size());
        Iterator<String> iter = mWbList.iterator();
        while (iter.hasNext()) {
            String s = iter.next();
            if (!mWbListFilter.contains(s)) {
                iter.remove();
            }
        }
        setWbIcons();
        Log.i("filterWbEffect", "mWbList_size" + mWbList.size());
    }

    private int findProIndex(String proValue, List<String> proValues) {
        int find = 0; // if not find, return 0
        if (proValues != null) {
            int len = proValues.size();
            for (int i = 0; i < len; i++) {
                if (proValues.get(i).equals(proValue)) {
                    find = i;
                    break;
                }
            }
        }
        return find;
    }

    private int findProIndex(int proValue, List<Integer> proValues) {
        int find = 0;
        if (proValues != null) {
            int len = proValues.size();
            for (int i = 0; i < len; i++) {
                if (proValues.get(i) == proValue) {
                    find = i;
                    break;
                }
            }
        }
        return find;
    }

    private boolean isValidIndex(int index, int listSize) {
        boolean valid = false;
        if (index >= 0 && index < listSize) {
            valid = true;
        }
        Log.i(TAG, "isValidIndex(" + index + ") reture " + valid);
        return valid;
    }

    private void refreshOptions(int index) {
        mCurrentOption = index;
        getCameraProParameters(true);
        setOptionBar();
    }

    private void refreshMf() {
        mOptionViews.get(PROF_OPTION_MF).setEnabled(mMfList.size() > 0);
    }

    public boolean handleMenuEvent() {
        boolean handle = false;
        if (isEnabled() && isShowing()) {
            handle = true;
        }
        return handle;
    }

    public boolean isShowCameraProContainer() {
        return true;
    }

    public void onCameraSwitch() {
        mMfList.clear();
        if (PROF_OPTION_MF < mOptionViews.size()) {
            refreshMf();
            if (mCurrentOption == PROF_OPTION_MF) {
                refreshOptions(PROF_OPTION_WB);
            }
        }
    }

    public void onAutoFocusing() {
        mMfIndex = 0;
        if (mCurrentOption == PROF_OPTION_MF && mOptionBar != null && mCurrent != null) {
            mOptionBar.setRangePinsByValue(0, 0);
            mCurrent.setText(AF);
        }
    }
}
